<?php
namespace Home\Controller;
use Think\Controller;
class ReferralController extends AdminAuthController {
    /**
     * 转诊记录列表
     */
    public function referrallist(){
        $timestart1 = date('Y-m-1');
        $timeend1   = date('Y-m-d');
        $M_list     = M('referral');
        $M_category = M('category');
        $M_state    = M('state');
        $type       = I('get.type') ? I('get.type') : 1;
        $time       = I('get.time');
        if(I('get.pname')){
            $pname = I('get.pname');
            $where['pname']     = array('like', "%$pname%");
        }
        if(I('get.cateid')){
            $where['cateid']       = I('get.cateid');
        }
        if(I('get.state')){
            $where['state']     = I('get.state');
        }
        if(I('get.timestart')){
            $timestart1  = I('get.timestart').' 00:00:00';
        }
        if(I('get.timeend')){
            $timeend1    = I('get.timeend').' 23:59:59';
        }
        if(I('get.type') == 1){
            if($timestart1 && $timeend1){
                $where['addtime'] = array(array('EGT',$timestart1),array('ELT',$timeend1,'AND'));
            }
        }else{
            if(I('get.time') == 1){//1-3月
                $timestart1  = date('Y-01-01',time()).' 00:00:00';
                $timeend1    = date('Y-03-31',time()).' 23:59:59';
            }
            if(I('get.time') == 2){//4-6月
                $timestart1  = date('Y-04-01',time()).' 00:00:00';
                $timeend1    = date('Y-06-30',time()).' 23:59:59';
            }
            if(I('get.time') == 3){//7-9月
                $timestart1  = date('Y-07-01',time()).' 00:00:00';
                $timeend1    = date('Y-09-30',time()).' 23:59:59';
            }
            if(I('get.time') == 4){//10-12月
                $timestart1  = date('Y-10-01',time()).' 00:00:00';
                $timeend1    = date('Y-12-31',time()).' 23:59:59';
            }
            if(I('get.time') == 5){//1-12月
                $timestart1  = date('Y-01-01',time()).' 00:00:00';
                $timeend1    = date('Y-12-31',time()).' 23:59:59';
            }
            if(I('get.time') == 6){//1-6月
                $timestart1  = date('Y-01-01',time()).' 00:00:00';
                $timeend1    = date('Y-06-31',time()).' 23:59:59';
            }
            if(I('get.time') == 7){//7-12月
                $timestart1  = date('Y-07-01',time()).' 00:00:00';
                $timeend1    = date('Y-12-31',time()).' 23:59:59';
            }
            $where['addtime'] = array(array('EGT',$timestart1),array('ELT',$timeend1,'AND'));
        }
        $timestart1     = date('Y-m-d',strtotime($timestart1));
        $timeend1       = date('Y-m-d',strtotime($timeend1));
        $count = $M_list->where($where)->count();
        $Page = new \Think\Page1($count, 10);  // 实例化 分页类
        //分页跳转的时候保证查询条件
        $Page->parameter   .=   "timestart=".urlencode($timestart1).'&';
        $Page->parameter   .=   "timeend=".urlencode($timeend1).'&';
        $Page->parameter   .=   "type=".urlencode($type).'&';
        foreach($where as $key=>$val) {
            $Page->parameter   .=   "$key=".urlencode($val).'&';
        }
        $Page->parameter   .=   "pname=".urlencode($pname);

        $show       = $Page->show();// 分页显示输出

        $list = $M_list ->where($where)
                        ->limit($Page->firstRow.','.$Page->listRows)
                        ->order('addtime DESC')
                        ->select();
        //echo $M_list->getlastsql();
        $category       = $M_category->select();
        $statelist      = $M_state->select();

        $this->assign(array(
            'list'      => $list,
            'category'  => $category,
            'statelist' => $statelist,
            'timestart' => $timestart1,
            'timeend'   => $timeend1,
            'type'      => $type,
            'time'      => $time,
            'where'     => $where,
            'pname'     => $pname,
            'pageshow'  => $show
        ));
        $this->display();
    }
    /**
     * 转诊记录详情
     */
    public function referraldetail(){
        $id         = I('get.id','','intval');
        $M_list     = M('referral');
        $list       = $M_list->find($id);
        if($list['pid']){
            $p_list     = M('patient');
            $pinfo      = $p_list->find($id);
        }
        $this->list = $list;
        $this->pinfo = $pinfo;
        $this->display();
    }
    /**
     * 转诊状态列表
     */
    public function statelist() {
        $M_category = M('state');
        $category_rows = $M_category->select();
        $this->category_rows = $category_rows;
        $this->display();
    }
    /**
     * 添加状态
     */
    public function stateadd() {
        if(IS_POST){
            $M_category = M('state');
            $data['title'] = I('post.title');
            $data['addtime'] = date("Y-m-d H:i:s");
            $added_row = $M_category->add($data);
            if ($added_row) {
                $this->success('添加成功', U('statelist'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 删除状态
     */
    public function statedel($id) {
        $id = intval($id);
        $M_category = M('state');

        $cate_row = $M_category->find($id);
        if (!$cate_row) {
            $this->error('该状态不存在！');
        }

        $deled_cate = $M_category->delete($id);
        if ($deled_cate) {
            $this->success('状态删除成功！');
        } else {
            $this->error('状态删除失败！');
        }
    }
    /**
     * 修改状态
     */
    public function statemod() {
        $M_category = M('state');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_category->find($id);
            if (!$cate_row) {
                $this->error('该状态不存在！');
            }
            $cate_row['title'] = I('post.title');
            $updated_row = $M_category->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('statelist'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('get.id','','intval');
            $cate_row = $M_category->find($id);
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
    /**
     * 治疗标签列表
     */
    public function treatlist() {
        $M_list = M('treat');
        $list = $M_list->select();
        $this->list = $list;
        $this->display();
    }
    /**
     * 添加治疗标签
     */
    public function treatadd() {
        if(IS_POST){
            $M_category = M('treat');
            $data['title'] = I('post.title');
            $data['addtime'] = date("Y-m-d H:i:s");
            $added_row = $M_category->add($data);
            if ($added_row) {
                $this->success('添加成功', U('treatlist'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 删除治疗标签
     */
    public function treatdel($id) {
        $id = intval($id);
        $M_category = M('treat');

        $cate_row = $M_category->find($id);
        if (!$cate_row) {
            $this->error('该标签不存在！');
        }

        $deled_cate = $M_category->delete($id);
        if ($deled_cate) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    /**
     * 修改治疗标签
     */
    public function treatmod() {
        $M_category = M('treat');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_category->find($id);
            if (!$cate_row) {
                $this->error('该标签不存在！');
            }
            $cate_row['title'] = I('post.title');
            $updated_row = $M_category->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('treatlist'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('get.id','','intval');
            $cate_row = $M_category->find($id);
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
    /**
     * 专项检查标签列表
     */
    public function checklist() {
        $M_list = M('check');
        $list = $M_list->select();
        $this->list = $list;
        $this->display();
    }
    /**
     * 添加专项检查标签
     */
    public function checkadd() {
        if(IS_POST){
            $M_category = M('check');
            $data['title'] = I('post.title');
            $data['addtime'] = date("Y-m-d H:i:s");
            $added_row = $M_category->add($data);
            if ($added_row) {
                $this->success('添加成功', U('checklist'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 删除专项检查标签
     */
    public function checkdel($id) {
        $id = intval($id);
        $M_category = M('check');

        $cate_row = $M_category->find($id);
        if (!$cate_row) {
            $this->error('该标签不存在！');
        }

        $deled_cate = $M_category->delete($id);
        if ($deled_cate) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    /**
     * 修改专项检查标签
     */
    public function checkmod() {
        $M_category = M('check');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_category->find($id);
            if (!$cate_row) {
                $this->error('该标签不存在！');
            }
            $cate_row['title'] = I('post.title');
            $updated_row = $M_category->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('checklist'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('get.id','','intval');
            $cate_row = $M_category->find($id);
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
}